package view;




import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import model.Car;
import model.CityMap;
import view.sprite.AddingCarSprite;
import view.sprite.AddingCarSprite.TYPE;
import view.sprite.BaseSprite;
import view.sprite.BuildingSprite;
import view.sprite.CarSprite;
import view.sprite.StreetSprite;

import com.trolltech.qt.core.QPoint;
import com.trolltech.qt.core.QRect;
import com.trolltech.qt.gui.QImage;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QMouseEvent;
import com.trolltech.qt.gui.QPaintEvent;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.gui.QResizeEvent;
import com.trolltech.qt.gui.QWidget;

public class CityWidget extends QWidget {
		
	private QImage bg;

	private final List<BaseSprite> mapSprites;
	private final List<CarSprite> carSprites;
	
	private final List<AddingCarSprite> addingCarSprites;
	
	private QPoint tmpCar;
	private QPoint tmpDest;
		
	private QPainter pPainter ;
	
	private int citySize;
	
	private boolean addingDestination;

	
	
	public CityWidget(final QWidget parent) {
		super(parent);		
		
		addingDestination=false;
		
		carSprites = new ArrayList<CarSprite>();
		mapSprites = new ArrayList<BaseSprite>();	
		
		addingCarSprites = new ArrayList<AddingCarSprite>();
		
//		((MainWindow)parent()).setSighting(false);
		
		init();
	}
	

	public void init() {
//		this.bg=ImageLoader.get().getImage(ImageLoader.ROAD);
		((MainWindow) parentWidget()).resizeFor(bg);		
	}



	private void updateSpriteImages(){
		
		
		final CityMap tmp = ((MainWindow) parent()).getCityMap();
		
		citySize= tmp.getnCells();
		
		carSprites.clear();
		mapSprites.clear();	
		
		
		List<Car> cars = new ArrayList<Car>();
		cars.addAll(tmp.getCars());
		
		for (final Car c : cars) {
			carSprites.add(new CarSprite(c,c.getSkin()));
//			System.out.println(c.getCurPosIndex());
		}
		
		cars.clear();
		
		for (int i = 0; i < citySize; i++) {
			for (int j = 0; j < citySize; j++) {
				if (tmp.getTerrain(new Point(i, j))== CityMap.CELL_TYPE.PALACE) {
					mapSprites.add(new BuildingSprite(new QPoint(i, j)));
				}
				if (tmp.getTerrain(new Point(i, j))== CityMap.CELL_TYPE.ROAD) {
					mapSprites.add(new StreetSprite(new QPoint(i, j)));
				}
			}
		}
	}
	
	
    @Override
    protected void paintEvent(final QPaintEvent arg__1) {
        pPainter = new QPainter(this);    	
        pPainter.drawImage(this.rect(), bg);
        
        
        updateSpriteImages();   
        
        for (final BaseSprite b : mapSprites) {
        	b.setRect(this.width(),this.height(),citySize);			
        	pPainter.drawImage(b.getRect(),b.getCurrImage());			
        }
        
        for (final CarSprite car : carSprites) {
        	car.setRect(this.width(),this.height(),citySize);			
        	pPainter.drawImage(car.getRect(),car.getCurrImage());			
        }
        
        
        if (((MainWindow)parent()).isAddingCars()) {	
	        for (final AddingCarSprite a : addingCarSprites) {
	        	pPainter.drawImage(a.getRect(),a.getCurrImage());			
	        }
		}
        
        
        //test stampa car
//        tempCar1.setImgGoUp();
//        tempCar2.setImgGoRight();
//        tempCar3.setImgGoLeft();
//        pPainter.drawImage(tempCar1.getRect(this.width(),this.height(),tempCity),tempCar1.getCurrImage());
//        pPainter.drawImage(tempCar2.getRect(this.width(),this.height(),tempCity),tempCar2.getCurrImage());
//        pPainter.drawImage(tempCar3.getRect(this.width(),this.height(),tempCity),tempCar3.getCurrImage());
//        pPainter.drawImage(tempCar4.getRect(this.width(),this.height(),tempCity),tempCar4.getCurrImage());
 
        super.paintEvent(arg__1);
    }
    @Override
    protected void resizeEvent(QResizeEvent arg__1) {
    	super.resizeEvent(arg__1);
        if (((MainWindow)parent()).isAddingCars()) {	
	        for (final AddingCarSprite a : addingCarSprites) {
	        	a.setRect(this.width(),this.height(),citySize);
	        }
		}
    }
    
    
    @Override
    protected void mousePressEvent(QMouseEvent event) {
    	super.mousePressEvent(event);
    	
    	
    	
    	if(((MainWindow)parent()).isAddingCars()){
    		final BaseSprite pick = picking(event.x(),event.y());
    		if (pick!= null ) {
    			if(!addingDestination)
					if(!((MainWindow)parent()).getMenuBarControl().canAddCarToAddingList(pick.getPosition().x(),pick.getPosition().y())){
						return;
    			}
    			
    			final QRect pickRec = pick.getRect(); 
    			for ( AddingCarSprite a : addingCarSprites) {
					if(!addingDestination && a.getType()==TYPE.CAR && a.getRect().equals(pickRec)){
						QMessageBox.warning((QWidget) parent(), "Warning", "Car in same position");
						return;
					}
				}
    			
    			if (addingDestination) {
    				addingDestination=false;
    				AddingCarSprite tmp = new AddingCarSprite(TYPE.ARRIVE);
    				tmp.setRect(pickRec ); 
    				tmp.setPosition(pick.getPosition());
    				addingCarSprites.add(tmp);
    				repaint();    			
    				tmpDest=pick.getPosition();
    				((MainWindow)parent()).getMenuBarControl().addCarToAddingList(tmpCar.x(),tmpCar.y(),tmpDest.x(), tmpDest.y());
				} else {//adding CAR
					
					addingDestination=true;
					AddingCarSprite tmp = new AddingCarSprite(TYPE.CAR);
					tmp.setRect(pickRec );    			
    				tmp.setPosition(pick.getPosition());
					addingCarSprites.add(tmp);
					repaint();
					tmpCar=pick.getPosition();
					
				}
			}
//    		((MainWindow)parent()).getMenuBarControl().addCarToAddingList(8, 3, 20, 22);
    				
    		
    	}
    }
    
    

	private BaseSprite picking(int x, int y) {
    	if(inBound(x, y,new QRect(mapSprites.get(0).getRect().topLeft(),
    				mapSprites.get(mapSprites.size()-1).getRect().bottomRight()))
    	){
    		for (BaseSprite sprite : mapSprites) {
    			if(inBound(x,y,sprite.getRect())){
    				if(sprite instanceof BuildingSprite){
    					System.out.println("BUILDING");    					
    					return null;    	
    				}
    				return sprite;
    			}
    		}
    		System.out.println("INMAP");
    	}
    	
   		System.out.println("OUTMAP");
		return null;    	
	}


	private boolean inBound(int x, int y, QRect rect) {
    	if( 	x>= rect.x()&&
    			y>= rect.y()&&
    			x<= rect.bottomRight().x()&&
    			y<= rect.bottomRight().y()
    			)
    		return true;
    	else
    		return false;

	}

	public void clearAddingSprites() {
		addingCarSprites.clear();
		addingDestination=false;
	}
  


}
